﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;

namespace MetaTracker.ViewModel
{
    internal class UserSettingsTabViewModel : TrackerBaseViewModel
    {
        #region Data and Properties

        public bool HasWithings
        {
            get
            {
                return tracker.HasWithings;
            }
        }
        public bool HasFitbit
        {
            get
            {
                return tracker.HasFitbit;
            }
        }
        public bool HasFatSecret
        {
            get
            {
                return tracker.HasFatSecret;
            }
        }
        //public string WithingsStatus
        //{
        //    get
        //    {
        //        if (tracker.CurrentUser == null)
        //        {
        //            return "ERROR: No selected user";
        //        }
        //        if (string.IsNullOrEmpty(tracker.CurrentUser.FitbitOauthToken))
        //        {
        //            return "Not Connected";
        //        }
        //        return "Connected";
        //    }
        //}
        //public string FitbitStatus
        //{
        //    get
        //    {
        //        if (tracker.CurrentUser == null )
        //        {
        //            return "ERROR: No selected user";
        //        }
        //        if( string.IsNullOrEmpty(tracker.CurrentUser.FitbitOauthToken))
        //        {
        //            return "Not Connected";
        //        }
        //        return "Connected";
        //    }
        //}
        //public string FatSecretStatus
        //{
        //    get
        //    {
        //        if (tracker.CurrentUser == null)
        //        {
        //            return "ERROR: No selected user";
        //        }
        //        if (string.IsNullOrEmpty(tracker.CurrentUser.FitbitOauthToken))
        //        {
        //            return "Not Connected";
        //        }
        //        return "Connected";
        //    }
        //}

        public ServiceEnum WeightService
        {
            get
            {
                return tracker.WeightService;
            }
            set
            {
                tracker.WeightService = value;
            }
        }
        public ServiceEnum FoodIntakeService
        {
            get
            {
                return tracker.FoodIntakeService;
            }
            set
            {
                tracker.FoodIntakeService = value;
            }
        }
        public ServiceEnum ExerciseService
        {
            get
            {
                return tracker.ExerciseService;
            }
            set
            {
                tracker.ExerciseService = value;
            }
        }

        #endregion //Data and Properties

        #region Constructors
        internal UserSettingsTabViewModel()
        {
            
        }
        #endregion //Constructors

        #region Commands

        public ICommand RegisterWithingsCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => this.RegisterWithingsCommand,
                    (parameter) => DoRegisterWithingsCommand(parameter) ,
                    () => true);
            }
        }
        private void DoRegisterWithingsCommand(object parameter)
        {
            if (!HasWithings)
            {
                //Do Register
                View.RegisterWithingsView register = new View.RegisterWithingsView();
                register.ShowDialog();
            }
            else
            {
                MessageBoxResult confrimation = MessageBox.Show(
                    "Are you sure you want to disable Withings access, "
                    + "and delete all Withings data?", "Confirm Account Removal",
                    MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

                if (confrimation == MessageBoxResult.Yes)
                {

                    tracker.DeleteService(ServiceEnum.Withings);
                    //TODO: Delete any other withings datga stored

                    if (WeightService == ServiceEnum.Withings)
                    {
                        WeightService = ServiceEnum.None;
                    }
                }
            }
        }

        public ICommand RegisterFatSecretCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => this.RegisterFatSecretCommand,
                    (parameter) => DoRegisterFatSecretCommand(parameter) ,
                    () => true);
            }
        }
        private void DoRegisterFatSecretCommand(object paramter)
        {
            if (!HasFatSecret)
            {
                //Do Register
                View.RegisterFatSecretView register = new View.RegisterFatSecretView();
                register.ShowDialog();
            }
            else
            {
                MessageBoxResult confirmation = MessageBox.Show(
                    "Are you sure you want to disable FatSecret access, "
                    + "and delete all FatSecret data?", "Confirm Account Removal",
                    MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

                if (confirmation == MessageBoxResult.Yes)
                {
                    tracker.DeleteService(ServiceEnum.FatSecret);

                    if (WeightService == ServiceEnum.FatSecret)
                    {
                        WeightService = ServiceEnum.None;
                    }
                    if (FoodIntakeService == ServiceEnum.FatSecret)
                    {
                        FoodIntakeService = ServiceEnum.None;
                    }
                    if (ExerciseService == ServiceEnum.FatSecret)
                    {
                        ExerciseService = ServiceEnum.None;
                    }
                }
            }
        }

        public ICommand RegisterFitbitCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => this.RegisterFitbitCommand,
                    (parameter) => DoRegisterFitbitCommand(parameter),
                    () => true);
            }
        }
        private void DoRegisterFitbitCommand(object parameter)
        {
            if (!HasFitbit)
            {
                //Do Register
                View.RegisterFitbitView register = new View.RegisterFitbitView();
                register.ShowDialog();
            }
            else
            {
                MessageBoxResult confirmation = MessageBox.Show(
                    "Are you sure you want to disable Fitbit access, "
                    + "and delete all Fitbit data?", "Confirm Account Removal",
                    MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

                if (confirmation == MessageBoxResult.Yes)
                {
                    tracker.DeleteService(ServiceEnum.Fitbit);

                    if (WeightService == ServiceEnum.Fitbit)
                    {
                        WeightService = ServiceEnum.None;
                    }
                    if (FoodIntakeService == ServiceEnum.Fitbit)
                    {
                        FoodIntakeService = ServiceEnum.None;
                    }
                    if (ExerciseService == ServiceEnum.Fitbit)
                    {
                        ExerciseService = ServiceEnum.None;
                    }
                }
            }
        }

        #endregion //Commands
        
        #region Methods
        #endregion //Methods
    }
}
